<?php
	session_start();
	include "../db.php";
	$err = 0; /* To check whether or not any error is there in input or not. */
	
	if(!isset($_SESSION['username'])) 
		header("location:../login/login.php?msg=line_6"); /* Checks whether or not session is activated */
	else if($_SESSION['username']=="") 
		header("location:../login/login.php?msg=line_7");
	
	/* Check the referer */
	$referer = $_SERVER['HTTP_REFERER'];			/* 	Checks whether or not the file is */
	$ref = explode("/",$_SERVER['HTTP_REFERER']);	/* 	called from valid location or not */
	end($ref);										/* 						*/
	$ref1 = $ref[key($ref)];						/* 						*/
	if(strpos($referer,"create_team.php")<0) {					/* 						*/
		session_destroy();							/* 						*/
		header("location:../login/login.php?msg=line_15");		/* 						*/
	}
	/* Check the referer */
	
	extract($_POST);
	if(trim($team_name)=="" || trim($team_leader)=="" || trim($team_description)=="") {
		
		$err = 1;
		$msg = $msg."Please fill all the details.";
		header("location:create_team.php?msg=$msg");
	}
	else if($_SESSION['username']!=$team_leader) {
		session_destroy();
		header("location:../login/login.php?msg=line_23");
	}
	
	$leader_id = $_SESSION['user_id'];
	$date = date("Y-m-d");
	$add_team_query="insert into team(name,doc,leader_id,description) values('".$team_name."','".$date."','".$team_leader."','".$team_description."')";
	$add_team_rs = mysql_query($add_team_query);
	
	if(!$add_team_rs) {
		echo mysql_error();
		$err = 1;
	}
	else
	{
		$get_team_id_query = "select id from team where name = '$team_name'";
		$get_team_id_rs = mysql_query($get_team_id_query);
		if(!$get_team_id_rs) {
			echo mysql_error();
			$err = 1;
		}
		
		$get_team_id_row = mysql_fetch_assoc($get_team_id_rs);
		$team_id = $get_team_id_row['id'];
		$member1 = explode(',',$team_member);
		foreach($member1 as $m)
		{
			if(trim($m)!="") {
				$add_member_query = "insert into team_member(team_id,member_id,confirm,direction,doc) values ('".$team_id."','".$m."','1','1','".$date."')";
				$add_member_rs = mysql_query($add_member_query);
				if(!$add_member_rs) {
					echo mysql_error();
					$err = 1;
				}
			}
		}
		$current_user_username = $_SESSION['username'];
		if($err==0) header("location:all_team_of_student.php?username=$current_user_username");
		else echo "Some Problem";
	}
?>